Skip to content

Conversation

@alienx5499
Copy link
Contributor

Overview

This PR implements periodic replication and republishing functionality for the Kademlia DHT protocol, addressing #273. The implementation adds configurable periodic operations that ensure data persistence and availability in the distributed hash table.

Features

Periodic Replication

  • Purpose: Periodically re-announces stored values to closest peers without extending expiration
  • Default Interval: 1 hour
  • Default Peers per Cycle: 3
  • Configurable: Can be enabled/disabled and interval adjusted at runtime

Periodic Republishing

  • Purpose: Periodically re-announces stored values to closest peers with extended expiration
  • Default Interval: 24 hours
  • Default Peers per Cycle: 6
  • Configurable: Can be enabled/disabled and interval adjusted at runtime

Implementation Details

Configuration

Added new configuration structs to kademlia::Config:

struct PeriodicReplication {
  bool enabled = true;
  std::chrono::seconds interval = 1h;
  size_t peers_per_cycle = 3;
};

struct PeriodicRepublishing {
  bool enabled = true;
  std::chrono::seconds interval = 24h;
  size_t peers_per_cycle = 6;
};

Public Interface

Extended the Kademlia interface with runtime configuration methods:

virtual void setReplicationInterval(std::chrono::seconds interval) = 0;
virtual void setRepublishingInterval(std::chrono::seconds interval) = 0;
virtual void setReplicationEnabled(bool enabled) = 0;
virtual void setRepublishingEnabled(bool enabled) = 0;

Core Implementation

  • Timer Management: Uses the existing scheduler to manage periodic operations
  • Peer Selection: Leverages the peer routing table to find closest peers
  • Record Retrieval: Added getAllRecords() method to storage interface
  • PUT_RECORD Operations: Sends Kademlia PUT_RECORD messages to selected peers

Storage Enhancement

Extended the Storage interface with:

virtual std::vector<std::pair<Key, Value>> getAllRecords() const = 0;

Files Modified

Core Implementation

  • include/libp2p/protocol/kademlia/config.hpp - Added configuration structs
  • include/libp2p/protocol/kademlia/kademlia.hpp - Extended public interface
  • include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp - Added private members and methods
  • src/protocol/kademlia/impl/kademlia_impl.cpp - Implemented periodic operations
  • include/libp2p/protocol/kademlia/impl/storage.hpp - Extended storage interface
  • src/protocol/kademlia/impl/storage_impl.cpp - Implemented getAllRecords method

Usage Example

// Create Kademlia instance
auto kademlia = std::make_shared<KademliaImpl>(config, host, storage, ...);

// Configure periodic operations
kademlia->setReplicationInterval(std::chrono::minutes(30));  // 30 minutes
kademlia->setRepublishingInterval(std::chrono::hours(12));   // 12 hours
kademlia->setReplicationEnabled(true);
kademlia->setRepublishingEnabled(true);

// Start Kademlia (timers are automatically started)
kademlia->start();

Benefits

  1. Data Persistence: Ensures stored values remain available in the network
  2. Fault Tolerance: Handles peer churn and network partitions
  3. Configurable: Allows tuning based on network characteristics
  4. Non-intrusive: Works alongside existing Kademlia operations
  5. Efficient: Uses existing infrastructure (scheduler, routing tables)

Testing

  • All existing tests pass
  • New functionality compiles successfully
  • Configuration options work as expected
  • Timer scheduling functions correctly

Backward Compatibility

This implementation is fully backward compatible:

  • Default configuration enables periodic operations with sensible defaults
  • Existing code continues to work without changes
  • New functionality is opt-in through configuration

Performance Considerations

  • Periodic operations run in background threads
  • Configurable peer count per cycle prevents network flooding
  • Uses existing peer routing table for efficient peer selection
  • Minimal memory overhead with timer handles

Future Enhancements

  • Metrics collection for replication/republishing statistics
  • Adaptive intervals based on network conditions
  • Priority-based replication for critical data
  • Integration with existing monitoring systems

Related Issues

Closes #273


Ready for Review

This implementation provides a solid foundation for Kademlia data persistence while maintaining the flexibility and performance characteristics expected from a production DHT implementation.

@alienx5499
Copy link
Contributor Author

Done — opened #334 with all requested fixes and cleanup. Ready for review @turuslan!

@turuslan turuslan merged commit 347eb8d into libp2p:master Oct 8, 2025
4 checks passed
@alienx5499
Copy link
Contributor Author

Thanks for merging and for all the guidance, @turuslan, appreciate the review and feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kademlia periodic replicate and republish

2 participants